import request from "@/utils/request";

// 查询项目文件信息列表
export function listProjectFileInfo(query) {
  return request({
    url: "/production/projectFileInfo/list",
    method: "get",
    params: query,
  });
}

// 查询项目文件信息所有列表
export function listAllProjectFileInfo(query) {
  return request({
    url: "/production/projectFileInfo/listAll",
    method: "get",
    params: query,
  });
}

// 查询项目文件信息详细
export function getProjectFileInfo(id) {
  return request({
    url: "/production/projectFileInfo/getInfo/" + id,
    method: "get",
  });
}

// 新增项目文件信息
export function addProjectFileInfo(data) {
  return request({
    url: "/production/projectFileInfo/add",
    method: "post",
    data: data,
  });
}

// 新增项目文件多文件上传接口
export function addProjectFileInfoByFile({
  files,
  folderId,
  fileType,
  projectId,
  menuCategory,
}) {
  const formData = new FormData();
  // 支持多文件
  files.forEach((file) => {
    formData.append("files", file);
  });
  formData.append("folderId", folderId);
  formData.append("fileType", fileType);
  formData.append("projectId", projectId);
  if (menuCategory !== undefined && menuCategory !== null) {
    formData.append("menuCategory", menuCategory);
  }
  return request({
    url: "/production/projectFileInfo/addByFile",
    method: "post",
    data: formData,
    headers: {
      "X-Requested-With": "XMLHttpRequest",
      // 不要加Content-Type，axios会自动处理
    },
  });
}

// 修改项目文件信息
export function updateProjectFileInfo(data) {
  return request({
    url: "/production/projectFileInfo/edit",
    method: "post",
    data: data,
  });
}

// 删除项目文件信息
export function delProjectFileInfo(id) {
  return request({
    url: "/production/projectFileInfo/remove/" + id,
    method: "get",
  });
}

// 下载链接转换
export function fileLinkTransfer(url) {
  return request({
    url: "/file/cos/sts?keyUrl=" + encodeURIComponent(url),
    method: "get",
  });
}

// 预览链接转换
export function fileLinkLooking(url) {
  return request({
    url: "/file/cos/preview?keyUrl=" + encodeURIComponent(url),
    method: "get",
  });
}

// 文件上传到云存储
export function addFileCloud(file) {
  const formData = new FormData();
  formData.append("file", file);
  return request({
    url: "/production/projectFileInfo/addFileCloud",
    method: "post",
    data: formData,
    headers: {
      "X-Requested-With": "XMLHttpRequest",
      // 不要加Content-Type，axios会自动处理
    },
  });
}
